
alter   PROC sp_release_reserved_lots( @lineItemNo int, @salesOrderId int, @processStepId int ) As
BEGIN

DECLARE @reserveLotCursor CURSOR
DECLARE @Lot_Id int
DECLARE @Qty_Allocated int
	
	SET @reserveLotCursor = CURSOR FOR SELECT Lot_Id, Qty_Allocated FROM Wip_Reservation_Table WHERE SalesOrder_Id = @salesOrderId and Line_Item_No = @lineItemNo 
	OPEN @reserveLotCursor
	FETCH NEXT FROM @reserveLotCursor INTO @Lot_Id, @Qty_Allocated
	WHILE @@FETCH_STATUS = 0
	BEGIN
		UPDATE Wip_Master SET Qty_Allocated = Qty_Allocated - ( ( ( Fg_Qty * @Qty_Allocated ) / ( select SUM(Fg_Qty) from Wip_Master where Fg_processStep_Id = @processStepId and lot_Id = @Lot_Id ) ) ) where lot_Id = @Lot_Id
		FETCH NEXT FROM @reserveLotCursor INTO @Lot_Id, @Qty_Allocated
	END
	CLOSE @reserveLotCursor
	DEALLOCATE @reserveLotCursor
	
	Delete from Backlog_Master where SalesOrder_Id = @salesOrderId and LineItem_No = @lineItemNo
	Delete from Wip_Reservation_Table where SalesOrder_Id = @salesOrderId and Line_Item_No = @lineItemNo
END
